<?php

namespace app\api\controller;

use app\common\logic\Data;
use think\facade\Validate;
use think\facade\Db;
use app\lib\Result;
use Yurun\Util\HttpRequest;

class SocketIo extends Base
{
  /**
   * 列表
   * @Author Qianlong <87498106@qq.com>
   * @PersonSite http://www.21ds.cn/
   */
  public function list()
  {
    $rule = [
      'pageNo'  => 'number',
      'pageSize'  => 'number',
    ];
    $data = [
      'pageNo' => input('param.pageNo', 1, 'trim'),
      'title' => input('param.title', '', 'trim'),
      'pageSize' => input('param.pageSize', 15, 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    // 构建查询条件
    $where = [];

    if (!empty($data['title'])) {
      $where[] = ['title|_id|app_id', 'like', $data['title']];
    }
    $limit = ($data['pageNo'] - 1) * $data['pageSize'];
    $dataList = Db::connect('mongodb')->name('socket_io')->field('*')->where($where)->order('create_time', 'desc')->limit($limit, $data['pageSize'])->select()->toArray();
    $totalcount = Db::connect('mongodb')->name('socket_io')->where($where)->count('_id');
    foreach ($dataList as &$vo) {
      $vo['create_time'] = date('Y-m-d H:i:s', $vo['create_time']);
    }
    $returnData['data'] = $dataList;
    $returnData['totalCount'] = $totalcount;
    $returnData['pageNo'] = (int) $data['pageNo'];
    $returnData['pageSize'] = (int) $data['pageSize'];
    return Result::Success($returnData);
  }
  public function all_list()
  {
    $where['uid'] = $this->uid;
    $dataList = Db::connect('mongodb')->name('socket_io')->field('_id,title')->where($where)->order('create_time', 'desc')->select()->toArray();
    return Result::Success($dataList);
  }
  public function editItemInfo()
  {
    if (request()->isPost()) {
      $rule = [
        'title|标题'  => 'require|max:32',
        'server_secret'  => 'require',
        'status|状态'  => 'require',
      ];
      $data = [
        'title' => input('param.title', '', 'trim'),
        'status' => input('param.status', '', 'trim'),
        '_id' => input('param._id', '', 'trim'),
        'server_secret' => input('param.server_secret', '', 'trim'),
      ];
      $validate = Validate::rule($rule);
      $result   = $validate->check($data);
      if (!$result) {
        return Result::Error($validate->getError());
      }
      $data['status'] = intval($data['status']);
      $wxCount = Db::connect('mongodb')->name('socket_io')->count();
      $this->security::checkVersionLimit($this->user['version'], 'socket_io_num', '实时通讯(Socket.io)数量', $wxCount);
      if (!empty($data['_id'])) {
        $this->redis->del('dever:socket.io:server:' . $data['_id'],'full');
        $status = Db::connect('mongodb')->name('socket_io')->where(['_id' => $data['_id']])->update($data);
        $tag = '编辑';
      } else {
        $data['create_time'] = time();
        $data['uid'] = $this->uid;
        $data['username'] = $this->username;
        $data['_id'] = createId('SI');
        $status = Db::connect('mongodb')->name('socket_io')->insert($data);
        $tag = '新增';
      }
      if ($status > 0) {
        return json(['code' => 200, 'data' => '', 'msg' => $tag . '成功']);
      } else {
        return json(['code' => -1, 'data' => '', 'msg' => $tag . '失败，请重试']);
      }
    }
  }
  public function delItem()
  {
    $rule = [
      'id'  => 'require',
    ];
    $data = [
      'id' => input('param.id', '', 'trim'),
    ];
    $validate = Validate::rule($rule);
    $result   = $validate->check($data);
    if (!$result) {
      return Result::Error($validate->getError());
    }
    $infoData = Db::connect('mongodb')->name('socket_io')->where('_id', $data['id'])->find();
    if (empty($infoData)) {
      return Result::Error('内容不存在或已删除');
    }
    $status = Db::connect('mongodb')->name('socket_io')->where('_id', $data['id'])->delete();
    Db::connect('mongodb')->name('wechat_media')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('mp_menu_list')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('socket_io_keyword')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('socket_io_sub')->where('mw_id', $data['id'])->delete();
    Db::connect('mongodb')->name('socket_io_dg')->where('mw_id', $data['id'])->delete();
    if ($status > 0) {
      return Result::Success('', '删除成功');
    } else {
      return Result::Error('删除失败，请稍后重试');
    }
  }
}
